/**  
* Copyright (c) 2009, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// created by noellynch
// Sep 11, 2009

package com.sceneworker.editors.beanshelleditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;

import com.jme.scene.Spatial;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.editors.snippets.Snippet;
import com.sceneworker.editors.snippets.SnippetManager;
import com.sceneworker.monitorpopup.panels.SimpleYesNoDlg;
import com.sceneworker.monitorpopup.panels.SimpleYesNoDlg.YesNoListener;

abstract	public class BeanShellEditorPanel extends JPanel {

	protected		JDialog			mcl_par;
	protected		JTextArea		mcl_text;
	protected		JTextArea		mcl_err;
	protected		Spatial			mcl_spatial;
	
	public	BeanShellEditorPanel(String str_defaultScript, JDialog cl_par, Spatial cl_s, final SnippetManager cl_mgr) {
		super();
		
		JMenuBar		lcl_menuBar = new JMenuBar();
		cl_par.setJMenuBar(lcl_menuBar);
		
		ActionListener		lcl_a = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Snippet		lcl_s = cl_mgr.getSnippetByTitle(e.getActionCommand());
				System.out.println("snippet = " + lcl_s.getLocation() + lcl_s.getName());
				//mcl_image = ImageIO.read(SimpleRadioButton.class.getClassLoader().getResource(str_imageLocation));
				URL			lcl_loc = BeanShellEditorPanel.class.getClassLoader().getResource(lcl_s.getLocation() + lcl_s.getName());
				String		lstr_temp = GenericUtils.getATextFile(lcl_loc);
				mcl_text.setText(lstr_temp);
			}
		};
		
		cl_mgr.initialiseSnippets();
		cl_par.getJMenuBar().add(cl_mgr.initialiseASnippetMenu(lcl_a));
		
		mcl_par = cl_par;
		mcl_spatial = cl_s;
		setLayout(new MigLayout("inset 5"));
		
		mcl_text = new JTextArea();
		mcl_text.setTabSize(3);
		mcl_text.setText(str_defaultScript);
		JScrollPane		lcl_scroller = new JScrollPane(mcl_text);
		add(lcl_scroller, "wrap, height 70%, width 100%");
		
		mcl_err = new JTextArea();
		JScrollPane		lcl_scroller2 = new JScrollPane(mcl_err);
		add(lcl_scroller2, "wrap, height 25%, width 100%");
		
		buildButtonPanel();
	}
	
	private	void	writeScriptToFile(File cl_f) {
		try {
			FileWriter		lcl_out = new FileWriter(cl_f);
			
			lcl_out.write(mcl_text.getText());
			
			lcl_out.flush();
			lcl_out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private		void	buildButtonPanel() {
		JButton		lcl_save = SwingComponentFactory.makeAButton("Save");
		lcl_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e1) {
				final	File	lcl_f = GenericUtils.getAFile(null, false, true);
				if(lcl_f == null) { return; }
				
				if(lcl_f.exists()) {
					SimpleYesNoDlg		lcl_dlg = new SimpleYesNoDlg((JFrame)null, 300, 300, "Overwrite", "File already exists", "Cancel", new YesNoListener() {
						public void dialogDone(SimpleYesNoDlg cl_dlg, boolean b_ok) {
							if(b_ok) {
								lcl_f.delete();
								writeScriptToFile(lcl_f);
							}
						}
					});
					lcl_dlg.setVisible(true);
				} else {
					writeScriptToFile(lcl_f);
				}
				
				return;
			}
		});
		
		JButton		lcl_load = SwingComponentFactory.makeAButton("Load");
		lcl_load.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File		lcl_file = GenericUtils.getAFile(null, false, false);
				
				if(lcl_file == null) {
					return;
				}
				
				mcl_text.setText(GenericUtils.getATextFile(lcl_file));
			}
			
		});
		
		JButton		mcl_done = SwingComponentFactory.makeAButton("Done");
		mcl_done.addActionListener(new ActionListener() {
			public	void
			actionPerformed(ActionEvent cl_e) {
				mcl_par.setVisible(false);
			}
		});
		
		
		JButton		lcl_update = SwingComponentFactory.makeAButton("Execute");
		lcl_update.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				executeScript();
			}
			
		});
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_update, mcl_done, lcl_load, lcl_save);
		
	}
	
	abstract	protected	void	executeScript();
}
